import Vue from "vue";
import VueRouter from "vue-router";
import Main from '@/views/Main.vue'
import Login from "@/views/Login.vue";
import Cookie from "js-cookie";

Vue.use(VueRouter)

const router = new VueRouter({
    routes: [
        {
            path: '/',
            component: Main,
            name: 'Main',
            redirect: '/home',
            children: [

                // { path: '/home', name: 'home', component: Home },
                // { path: '/user', name: 'user', component: User },
                // { path: '/mall', name: 'mall', component: Mall },
                // { path: '/page1', name: 'page1', component: Page1},
                // { path: '/page2', name: 'page2', component: Page2}
            ]
        },
        {
            path: '/login',
            name: 'login',
            component: Login
        }

    ]
})

router.beforeEach((to, from, next) => {
    const token = Cookie.get('token')
    // 如果token 不存在，且当前页面不在登录页，则跳转到登录页
    if(!token && to.name !== 'login') {
        next({name: 'login'})
    }else if(token && to.name === 'login') {
        next({name: 'home'})
    }else {
        next()
    }
})
// router.beforeEach((to, from, next) => {
//     next()
// })


export default router